﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Threading;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;
using Telerik.Web.UI;

/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{
	public virtual string getHeader(DataControlFieldCollection dt)
	{
		StringBuilder sb = new StringBuilder();

		sb.Append("<table  name='headertable' class='stats2' cellspacing='0' rules='all' border='1' id='headertable' style='border-collapse:collapse;' >");
		sb.Append("<tr class='hed2'>");

		foreach (DataControlField dc in dt)
		{
			sb.AppendFormat("<th scope='col' style='width:{1}'>{0}</th>", dc.HeaderText, dc.ItemStyle.Width);
		}

		sb.Append("</tr>");
		sb.Append("</table>");

		return sb.ToString();
	}

	protected void initData()
	{
		if(!Thread.CurrentPrincipal.Identity.IsAuthenticated)
			Response.Redirect("login.aspx");

		if (Session["CurrentTeacherInfo"] == null)
		{
			string message;

			DataLayer dal = new DataLayer();
			Teacher teacher = dal.ReadTeacher(
				PAConfiguration.ConnectionString,
				decimal.Parse(Thread.CurrentPrincipal.Identity.Name),
				out message);
			PALogManager.MessageLog(message);

			if(teacher == null)
				Response.Redirect("~login.aspx");
			else
				SetCurrentTeacher(teacher);
		}
	}

	#region Parameters
	protected decimal SectionId
	{
		get
		{
			if (string.IsNullOrEmpty(Request.QueryString["sectionId"]))
				return -1;

			return decimal.Parse(Request.QueryString["sectionId"]);
		}
	}

	protected void ApplySortingToHeader( TableCellCollection cells)
	{
		foreach (TableCell cell in cells)
		{
			if (!cell.HasControls())
				continue;

			LinkButton button = cell.Controls[0] as LinkButton;

			if (button == null)
				continue;


			if (button.CommandArgument == SortExpression)
			{


				Image img = new Image();
				img.ImageUrl = "images/default.gif";

				if (isAsc)
				{
					img.ImageUrl = "images/darrow.gif";
				}
				else
				{
					img.ImageUrl = "images/uarrow.gif";
				}
				cell.Controls.Add(img);
			}
		}
	}

	public virtual string Icon
	{
		get
		{
			return "images/current_sections_icon.png";
		}
	}
	protected decimal StudentId
	{
		get
		{
			if (string.IsNullOrEmpty(Request.QueryString["studentId"]))
				return -1;

			return decimal.Parse(Request.QueryString["studentId"]);
		}
	}
	#endregion

	protected void SecurityCheck()
	{
		if (Teacher.IS_CALL_CENTER == 1)
		{
			Response.Redirect("alerts.aspx");
		}
	}

	public void CheckSections(DataTable data)
	{
		if (data != null)
		{
			if (data.Rows.Count == 0)
			{
				Response.Redirect("NoSections.aspx");
			}
		}
		
	}

	#region Session Data
	public Teacher Teacher
	{
		get
		{
			initData();
			return (Teacher)Session["CurrentTeacherInfo"];
		}
	}

	public static Current sCurrentInfo
	{
		get
		{
			
			Current CurrentInfo = (Current)HttpContext.Current.Session["CurrentGeneralInfo"];


			return CurrentInfo;
		}
	}

	public Current CurrentInfo
	{
		get
		{

			initData();
			Current CurrentInfo = (Current)Session["CurrentGeneralInfo"];

			return CurrentInfo;
		}
	}


	public void SetCurrentTeacher(Teacher teacher)
	{
		Session["CurrentTeacherInfo"] = teacher;

		DataLayer dal = new DataLayer();

		String message = string.Empty;

		Session["CurrentGeneralInfo"] = dal.GetCurrent(PAConfiguration.ConnectionString, out message);
		PALogManager.MessageLog(message);
	}
	#endregion


	#region Sorting
	public string Sorting
	{
		get
		{
			return (String)ViewState["Sorting"];
		}
		set
		{
			ViewState["Sorting"] = value;
		}
	}

	public string SortExpression
	{
		get
		{
			return (String)ViewState["SortExpression"];
		}
		set
		{
			ViewState["SortExpression"] = value;
		}
	}

	public bool isAsc
	{
		get
		{
			if (ViewState["Direction"] == null)
				ViewState["Direction"] = true;

			return (bool)ViewState["Direction"];
		}
		set
		{
			ViewState["Direction"] = value;
		}
	}


	protected void Sort(GridSortCommandEventArgs e)
	{
		if (SortExpression != e.SortExpression)
			isAsc = true;
		else
			isAsc = !isAsc;
		

		Sorting = String.Format("{0} {1}",
			e.SortExpression,
			(isAsc ? "Asc" : "Desc")
			);

		SortExpression = e.SortExpression;

		
		Fill();
	}

	
	protected void Sort(GridViewSortEventArgs e)
	{
		if (SortExpression != e.SortExpression)
			isAsc = true;
		else
			isAsc = !isAsc;
		

		Sorting = String.Format("{0} {1}",
			e.SortExpression,
			(isAsc ? "Asc" : "Desc")
			);

		SortExpression = e.SortExpression;

		if (isAsc)
			e.SortDirection = SortDirection.Ascending;
		else
			e.SortDirection = SortDirection.Descending;

		Fill();
	}
	#endregion
	
  protected void Page_Load(object sender, EventArgs e)
	{
		if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
		{
			if (Teacher.CHANGE_PWD_FLAG != 0)
			{
				Response.Redirect("ChangePassword.aspx");
			}
		}

		pageLoad();
	}

	protected virtual void pageLoad()
	{
	}

	protected virtual void Fill()
	{
	}

  public bool IsAdminPage()
  {
    string namePage = Page.GetType().FullName;
    bool result = (namePage == "ASP.teachers_aspx") || (namePage == "ASP.students_aspx") || 
      (namePage == "ASP.settings_aspx") || (namePage == "ASP.blastoffreport_aspx") || (namePage == "ASP.rainbowreport_aspx") ||
      (namePage == "ASP.teachertrackerreport_aspx") || (namePage == "ASP.emaildeliveryreport_aspx") || (namePage == "ASP.emaildeliverydetailsreport_aspx");
    return result;
  }

  public string GetAdminPageName()
  {
    string result = "Teachers";

    string namePage = Page.GetType().FullName;
    if (namePage == "ASP.teachers_aspx")
      result = "Teachers";
    else if (namePage == "ASP.students_aspx")
      result = "Students";
    else if (namePage == "ASP.settings_aspx")
      result = "Settings";
    else if ((namePage == "ASP.blastoffreport_aspx") || (namePage == "ASP.rainbowreport_aspx") ||
      (namePage == "ASP.teachertrackerreport_aspx") || (namePage == "ASP.emaildeliveryreport_aspx") || 
      (namePage == "ASP.emaildeliverydetailsreport_aspx"))
      result = "Reports";
    
    return result;
  }
}
